using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
  public   class tbkhoanggiaDAL
    {
       private SqlConnection cn = null;
        public tbkhoanggiaDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbkhoanggiaDAL(tbkhoanggia khoanggia )
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbkhoanggia khoanggia)
        {
            SqlCommand cm = new SqlCommand("khoanggia_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenkhoanggia", khoanggia .Tenkhoanggia );
            cm.Parameters.AddWithValue("@giatoithieu", khoanggia .Giatoithieu );
            cm.Parameters.AddWithValue("@giatoida", khoanggia .Giatoida );
            cm.ExecuteNonQuery();
        }
        public void Update(tbkhoanggia khoanggia)
        {
            SqlCommand cm = new SqlCommand("khoanggia_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makhoanggia",khoanggia .Makhoanggia );
            cm.Parameters.AddWithValue("@tenkhoanggia",khoanggia .Tenkhoanggia );
            cm.Parameters.AddWithValue("@giatoithieu",khoanggia .Giatoithieu);
            cm.Parameters.AddWithValue("@giatoida", khoanggia .Giatoida );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("khoanggia_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makhoanggia", iD);
            cm.ExecuteNonQuery();
        }
        public tbkhoanggiacollection  Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("khoanggia_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbkhoanggiacollection  khoanggiaCollection = new tbkhoanggiacollection ();
            while (dr.Read())
            {
                tbkhoanggia khoanggia = new tbkhoanggia ();
               khoanggia .Makhoanggia = int.Parse(dr.GetValue(0).ToString());
               khoanggia .Tenkhoanggia   = (dr.GetValue(1).ToString());
               khoanggia .Giatoithieu = int.Parse(dr.GetValue(2).ToString());
               khoanggia .Giatoida = int.Parse(dr.GetValue(3).ToString());
                khoanggiaCollection   .Add(khoanggia );
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khoanggiaCollection   ;
        }
    }
}
